package com.sanjiahantang.youjiarole.module.programmeprice.views.impl;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.module.goods.beans.GoodsAttrBean;
import com.sanjiahantang.youjiarole.module.goods.beans.GoodsBean;
import com.sanjiahantang.youjiarole.module.programmeprice.adapters.ProgramAdapter;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramBean;
import com.sanjiahantang.youjiarole.module.programmeprice.presenters.ProgramPricePresenter;
import com.sanjiahantang.youjiarole.module.programmeprice.views.ProgramPriceView;
import com.sanjiahantang.youjiarole.utils.FileUtils;

import static com.sanjiahantang.youjiarole.config.ConfigValue.param;


/**
 * Created by admin on 2017/10/25.
 */

public class ProgramPriceActivity extends BaseMvpActivity<ProgramPriceView, ProgramPricePresenter> implements ProgramPriceView, View.OnClickListener {

    private TextView title, txtRight, txtsend, txtsave, txtTotal;
    private View flLeft, flIcon3,llSelectAll;
    private RecyclerView recyclerView;
    private CheckBox select_all_delete;
    private String ppid;
    private String PRICE_ORDER_IS;
    private ProgramBean.Data data;
    private ProgramAdapter adapter;
    private String version;
    private int type;

    @Override
    public ProgramPricePresenter initPresenter() {
        return new ProgramPricePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_programprice);
        title = (TextView) findViewById(R.id.title);
        findViewById(R.id.fl_Left).setOnClickListener(this);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        txtRight = (TextView) findViewById(R.id.txtRight);
        txtsave = (TextView) findViewById(R.id.txtsave);
        txtsend = (TextView) findViewById(R.id.txtsend);
        txtsend.setOnClickListener(this);
        txtsave.setOnClickListener(this);
        flIcon3 = findViewById(R.id.flIcon3);
        flIcon3.setVisibility(View.VISIBLE);
        flIcon3.setOnClickListener(this);
        llSelectAll = findViewById(R.id.llSelectAll);
        txtTotal = (TextView) findViewById(R.id.txtTotal);
        if (ConfigValue.param != null){
            txtRight.setText("");
            findViewById(R.id.llBottom).setVisibility(View.GONE);
            ConfigValue.param = null;
        }else{
            txtRight.setText("编辑");
            findViewById(R.id.llBottom).setVisibility(View.VISIBLE);
        }

        title.setText("方案报价清单");
        version=getIntent().getStringExtra(KEY_VERSION);
       // type=getIntent().getIntExtra(KEY_TYPE,0);
        select_all_delete = (CheckBox) findViewById(R.id.select_all_delete);
        select_all_delete.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isCheck) {
                if (isCheck==false) {
                    select_all_delete.setChecked(false);
                    presenter.changData($this,SWITCH_SELECT_NONE);
                }else {
                    select_all_delete.setChecked(true);
                    presenter.changData($this,SWITCH_SELECT_ALL);
                }
            }
        });

        presenter.setOnLoadListener(new OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] param = (Object[]) content;
                switch ((int) param[0]) {
                    case CALLBACK_TYPE_RECYCLER_ADAPTER:
                        recyclerView.setAdapter((RecyclerView.Adapter) param[1]);
                        adapter = (ProgramAdapter) param[1];
                        break;
                    case CALLBACK_TYPE_QUICK_ADAPTER:
                        txtTotal.setText(String.format("%.2f", param[1]));
                        break;
                    case MeCaseDetail_LIST_DATA:
                        data = (ProgramBean.Data) param[1];
                        break;
                    case CALLBACK_TYPE_BOOLEAN:
                        if ((Boolean) param[1]==true){
                            select_all_delete.setBackgroundResource(R.drawable.i_radio_on);
                        }else {
                            select_all_delete.setBackgroundResource(R.drawable.i_radio_off);
                        }
                        break;
                    case CALLBACK_TYPE_STRING:
                        title.setText((CharSequence) param[1]);
                        break;
                }

            }
        });
        ppid = getIntent().getStringExtra("requireId");
        PRICE_ORDER_IS = getIntent().getStringExtra("PRICE_ORDER_IS");//取出数据判断按钮是否隐藏
    }

    @Override
    public void initData() {
        presenter.LoadBaoJList(this, ppid,version,0);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.fl_Left:
                finish();
                break;
            case R.id.flIcon3://编辑
                llSelectAll.setVisibility(View.VISIBLE);
                flIcon3.setVisibility(View.INVISIBLE);
                txtsave.setVisibility(View.VISIBLE);
                txtsend.setVisibility(View.GONE);
                presenter.changData(this,SWITCH_EDIT);
                break;
            case R.id.txtsave://保存
                txtsend.setVisibility(View.VISIBLE);
                txtsave.setVisibility(View.GONE);
                flIcon3.setVisibility(View.VISIBLE);
                llSelectAll.setVisibility(View.GONE);
                presenter.changData(this, SWITCH_NO_EDIT);
                break;
            case R.id.txtsend://检查版本是否过期推送给用户
                presenter.checkCustomerIsOrder(this,String.valueOf(ppid), new OnLoadListener() {
                    @Override
                    public void onLoad(Object content) {
                        presenter.savePlanPriceInfo($this,adapter.getData().getUserRequireId(),ppid);
                    }
                });
                break;
        }
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        /**
         * 1.skuFaId
         2.planId方案id  long
         3.userRequireId 需求id long
         4.serviceName 服务名称 String
         5.servicePrice 单价 String
         6.serviceNum 数量 int
         7.serviceCount 合计 String
         8.typeId 1设计师 2客户3项目经理（分身份类型）
         9.serviceType 0报价清单1增项int
         10.isRecommend 是否推荐 0int
         11.mgiId 商品id long
         */
        if (param != null){
            Object[] objects = (Object[]) param;
            GoodsBean.Data obj = (GoodsBean.Data) objects[0];
            GoodsAttrBean.SkuData  sku = (GoodsAttrBean.SkuData) objects[1];
            String  number = (String) objects[2];
            String issimilar= (String) objects[3];
            switch (obj.getTypeGoodsId()){
                case 2://家具
                    ProgramBean.Data.GoodsListBean beans=new ProgramBean.Data.GoodsListBean();
                    beans.setSkuFaId(sku.getSkuId());
                    beans.setTypeId(""+0);
                    beans.setGoodsName(obj.getGoodsName());
                    beans.setSupplyName(obj.getSupplyName());
                    beans.setBrandType(sku.getMgpiValue());
                    beans.setGoodsPrice(""+(sku.getSkuPrice()+obj.getBasePrice()));
                    beans.setGoodsNum(Integer.valueOf(number));
                    beans.setGoodsCount(String.valueOf((sku.getSkuPrice() + obj.getBasePrice())* Integer.valueOf(number)));
                    beans.setGoodsType(String.valueOf(obj.getTypeGoodsId()-1));
                    beans.setImageUrl(FileUtils.rePath(obj.getUrl()));
                    beans.setIsRecommend(1);
                    beans.setSupplyId(obj.getSupplyId());
                    beans.setMgiId(obj.getMgiId());
                    beans.setPlanId(Integer.valueOf(ppid));
                    adapter.addCell(beans,1,issimilar);
                    break;
                case 3://配飾
                    ProgramBean.Data.GoodsListBean ps_beans=new ProgramBean.Data.GoodsListBean();
                    ps_beans.setSkuFaId(sku.getSkuId());
                    ps_beans.setTypeId(""+0);
                    ps_beans.setGoodsName(obj.getGoodsName());
                    ps_beans.setSupplyName(obj.getSupplyName());
                    ps_beans.setBrandType(sku.getMgpiValue());
                    ps_beans.setGoodsPrice(""+(sku.getSkuPrice()+obj.getBasePrice()));
                    ps_beans.setGoodsNum(Integer.valueOf(number));
                    ps_beans.setGoodsCount(String.valueOf((sku.getSkuPrice() + obj.getBasePrice())* Integer.valueOf(number)));
                    ps_beans.setGoodsType(String.valueOf(obj.getTypeGoodsId()-1));
                    ps_beans.setImageUrl(FileUtils.rePath(obj.getUrl()));
                    ps_beans.setIsRecommend(1);
                    ps_beans.setSupplyId(obj.getSupplyId());
                    ps_beans.setMgiId(obj.getMgiId());
                    ps_beans.setPlanId(Integer.valueOf(ppid));
                    adapter.addCell(ps_beans,2,issimilar);
                    break;

            }

            param = null;
        }
    }
}
